<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第3步：选择商品图片</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="商品相册" prop="albumId">
        <el-select v-model="ruleForm.albumId" placeholder="请选择">
          <el-option
              v-for="item in albumListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
        <div>暂时略过：显示相册中的图片、向相册中上传图片……</div>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      albumListOptions:[],
      ruleForm: {
        albumId: '',
        albumName: ''
      },
      rules: {
        albumId: [
          {type: 'number', required: true, message: '请选择相册', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    backToPreStep() {
      this.$router.push('spu-add-new2');
    },
    getPreRuleForm() {
      let ruleFormString = localStorage.getItem('ruleForm');
      console.log('ruleFormString = ' + ruleFormString);
      let preRuleForm = JSON.parse(ruleFormString);
      console.log('preRuleForm：');
      console.log(preRuleForm);

      if (!preRuleForm || !preRuleForm.brandId) {
        console.log('在LocalStorage中没有检测到前序步骤的数据，即将清除可能存在的数据，并返回第2步……')
        this.$router.push('spu-add-new2');
        return;
      }
      this.ruleForm.albumId = preRuleForm.brandId;
      this.ruleForm = preRuleForm;
    },
    loadAlbumList() {
      console.log('准备从服务器端加载【相册列表】……');
      let url = 'http://localhost:9080/albums';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.albumListOptions = responseBody.data;
          });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('准备从服务器端加载【相册详情】……');
          let url = 'http://localhost:9080/albums/' + this.ruleForm.albumId;
          console.log('请求路径：' + url);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url)
              .then((response) => {
                console.log('接收到服务器端响应的结果：');
                let responseBody = response.data;
                console.log(responseBody);
                if (responseBody.state == 20000) {
                  this.ruleForm.albumName = responseBody.data.name;
                  let ruleFormString = JSON.stringify(this.ruleForm);
                  console.log('【Step-3】ruleFormString = ' + ruleFormString);
                  localStorage.setItem('ruleForm', ruleFormString);
                  this.$router.push('spu-add-new4');
                } else {
                  console.log(responseBody.message);
                  this.$message.error(responseBody.message);
                }
              });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.getPreRuleForm();
    this.loadAlbumList();
  }
}
</script>

<style scoped>
</style>